package com.example.demo.tixiban;

import java.util.Stack;

/**
 * @author xujm
 * @version V2.4
 * @since 2.4.0 2022/2/20 15:13
 */
public class Stack_用栈实现一个队列 {


    static class MyQueue<V>{

        private Stack<V> pushStack;
        private Stack<V> popStack;

        public MyQueue() {
            this.pushStack = new Stack<>();
            this.popStack = new Stack<>();
        }

        private void pushToPop(){
            if(popStack.isEmpty()){
                while (!pushStack.isEmpty()){
                    popStack.push(pushStack.pop());
                }
            }
        }

        public void add(V value){
            pushStack.push(value);
        }

        public V remove(){
            if(!popStack.isEmpty()){
                return popStack.pop();
            }else{
                pushToPop();
            }
            return popStack.pop();
        }

        public boolean isEmpty(){
            return popStack.isEmpty() && pushStack.isEmpty();
        }

    }


    public static void main(String[] args) {

        MyQueue<Integer> queue = new MyQueue<Integer>();
        queue.add(1);
        queue.add(2);
        queue.add(3);
        while(!queue.isEmpty()){
            System.out.println(queue.remove());
        }

    }


}
